package yxy.game.pm2.active.command.mrlc;

import yxy.cherry.data.bean.actsigncharge;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_每日累冲;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActMrlcInfoRequest;
import yxy.game.pm2.message.PackageMessage;

import java.util.ArrayList;
import java.util.Map;

@MessageType(proto = Command.ActMrlcInfo, classtype = ActiveMessage.ActMrlcInfoRequest.class)
public class CMD_Active_每日累冲_Info extends Command<ActMrlcInfoRequest> {

	@Override
	public void execute(MessageSource messageSource, ActMrlcInfoRequest request) throws Exception {
		String pcode = messageSource.getTag();
		ActiveMessage.ActMrlcInfoResponse.Builder response = ActiveMessage.ActMrlcInfoResponse.newBuilder();
		
		ActiveModule_每日累冲 am = ActiveModule_每日累冲.get(pcode,false);
		if(am == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		response.setPayamount(am.getPayamounts());

		Map<String, actsigncharge> ascs = ActDataCenter.getAllData(actsigncharge.class, pcode);
		for(String key:ascs.keySet()) {
			actsigncharge asc = ascs.get(key);

			ActiveMessage.MrlcInfo.Builder buil = ActiveMessage.MrlcInfo.newBuilder();
			buil.setId(asc.getId());
			buil.setPaycondition(asc.getChargecondition());
			buil.setCanget(am.cangain(asc.getId()));
				
			ArrayList<Currency> curs = (ArrayList<Currency>) Currency.create(asc.getRewardtype(), asc.getRewardid(), asc.getRewardnum());
			for(int i = 0; i < curs.size(); i++) {
				Currency cur = curs.get(i);
				PackageMessage.Item.Builder item = PackageMessage.Item.newBuilder();
				item.setType(cur.getType().getNumber());
				item.setDataId(cur.getDataId());
				item.setNumber(cur.getNumber());
				buil.addRewards(item);
			}
			response.addMrlcinfo(buil);
		}
		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActMrlcInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
	
}
